package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.Classinfo;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.query.ClassinfoQuery;
import cn.wolfcode.rbac.service.IClassinfoService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassinfoController {
    
    @Autowired
    private IClassinfoService classinfoService;
    
    @Autowired
    private IEmployeeService employeeService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    //@RequiredPermission(name="部门列表")
    @RequiresPermissions(value={"classinfo:list","班级列表"},logical= Logical.OR)
    // qo 要贴注解，@ModelAttribute 将数据添加到模型对象中，等价于 model.addAttribute("attributeName", qo);
    public String list(Model model,@ModelAttribute("qo") ClassinfoQuery qo){
        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees",employees);

        PageInfo result = classinfoService.query(qo);
        model.addAttribute("result", result);
        return "classinfo/list"; // /WEB-INF/views/classinfo/list3.ftl
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody   // 告诉 Spring MVC，我们需要转为 json格式返回给前端
   // @RequiredPermission(name="部门保存或修改")
    @RequiresPermissions(value={"classinfo:saveOrUpdate","班级保存或修改"},logical= Logical.OR)
    public JsonResult saveOrUpdate(Classinfo classinfo){

            if(classinfo.getId() == null){ // 代表新增
                classinfoService.save(classinfo);
            }else { // 代表修改
                classinfoService.update(classinfo);
            }
            return new JsonResult();
        //return "redirect:/classinfo/list.do";  // http://localhost/classinfo/list.do
    }


    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value={"classinfo:delete","班级删除"},logical= Logical.OR)
    public JsonResult delete(Long id){

            if (id != null) {
                classinfoService.delete(id);
            }
            return new JsonResult();
        // return "redirect:/classinfo/list.do";  // http://localhost/classinfo/list.do
    }
}
